import store from '@/store/index.js';

var DeleteModelCommand = function() {
	
};

DeleteModelCommand.confirmMessage="确定要删除控件吗？"

DeleteModelCommand.prototype.getComponentModelById=function(id){
	var nodeModelArray = [];
	var componentModel = store.state.formDesigner.componentModel;
	nodeModelArray.push(componentModel);
	var firstComponentModel = nodeModelArray.shift();
	while (firstComponentModel != undefined) {
		if (firstComponentModel.id == id) {
			return firstComponentModel;
		}
		var children = firstComponentModel.children;
		if (children !== undefined) {
			for (var i = 0; i < children.length; i++) {
				nodeModelArray.push(children[i]);
			}
		}
		firstComponentModel = nodeModelArray.shift();
	}
};

DeleteModelCommand.prototype.getParentComponentModelById=function(id){
	var nodeModelArray = [];
	var componentModel = store.state.formDesigner.componentModel;
	nodeModelArray.push(componentModel);
	var firstComponentModel = nodeModelArray.shift();
	while (firstComponentModel != undefined) {
		var children = firstComponentModel.children;
		if (children !== undefined) {
			for (var i = 0; i < children.length; i++) {
				if(children[i].id==id){
					return firstComponentModel;
				}
				nodeModelArray.push(children[i]);
			}
		}
		firstComponentModel = nodeModelArray.shift();
	}
};

DeleteModelCommand.prototype.execute = function() {
	var component=store.state.formDesigner.getSelectedDesignComponent();
	var id=component.componentModel.id;
	var componentModel=this.getComponentModelById(id);
	if(componentModel.type=="page"){
		return;
	}
	var parentComponentModel=this.getParentComponentModelById(id);
	var index=-1;
	for(var i=0;i<parentComponentModel.children.length;i++){
		if(parentComponentModel.children[i].id==componentModel.id){
			index=i;
			break;
		}
	}
	parentComponentModel.children.splice(index,1);
	store.state.formDesigner.getPropertyTable().setSelectedComponent(null);
};
export default DeleteModelCommand